import request from "@/request/request.ts";
import { pageParams, responseRole } from "@/api/permissions/roleApi/interface.ts";
import { role } from "@/api/permissions/userApi/interface.ts";

enum Enum {
  fetchRoleList = "/admin/acl/role",
  addRole = "/admin/acl/role/save",
  updateRole = "/admin/acl/role/update",
  deleteRole = "/admin/acl/role/remove",
  batchRemoveRole = "/admin/acl/role/batchRemove",
  fetchPermissions = "/admin/acl/permission/toAssign",
  doAssignPer = "/admin/acl/permission/doAssign"
}

/* 获取角色列表的接口 */
export const fetchRoleListAPI = (pageParams: pageParams): Promise<responseRole> => {
  return request({
    url: `${Enum.fetchRoleList}/${pageParams.page}/${pageParams.limit}`,
    method: "GET",
    params: {
      roleName: pageParams.roleName,
    },
  });
};

/* 添加角色的接口或更新角色的接口 */
export const addOrUpdateRoleAPI = (data: role) => {
  if (data.id) {
    return request({
      url: Enum.updateRole,
      method: "PUT",
      data,
    });
  } else {
    return request({
      url: Enum.addRole,
      method: "POST",
      data,
    });
  }
};

/* 删除角色的接口 */
export const deleteRoleAPI = (id: number): Promise<any> => {
  return request({
    url: `${Enum.deleteRole}/${id}`,
    method: "DELETE",
  });
};


/* 批量删除角色的接口 */
export const batchRemoveRoleAPI = (idList: number[]): Promise<any> => {
  return request({
    url: Enum.batchRemoveRole,
    method: "DELETE",
    data: idList,
  });
};

/* 获取所有菜单权限和按钮权限的接口 */
export const fetchPermissionsAPI = (id: number): Promise<any> => {
  return request({
    url: `${Enum.fetchPermissions}/${id}`,
    method: "GET",
  });
};

/* 角色分配权限 */
export const doAssignPerAPI = ({ roleId, permissionId }:{roleId:any,permissionId:any}) => {
  return request({
    url: Enum.doAssignPer,
    method: "POST",
    params: {
      roleId,
      permissionId,
    },
  });
};
